/*
 * Daniel R Padilla
 *
 * Copyright (c) 2007, Daniel R Padilla
 *
 * This copyrighted material is made available to anyone wishing to use, modify,
 * copy, or redistribute it subject to the terms and conditions of the GNU
 * Lesser General Public License, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License
 * for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this distribution; if not, write to:
 * Free Software Foundation, Inc.
 * 51 Franklin Street, Fifth Floor
 * Boston, MA  02110-1301  USA
 */
package org.databasefacade.util;


import java.util.Calendar;
import java.util.GregorianCalendar;


public class DateUtil {
      
	public static java.util.Date getDate(Object data) {
		return null;
	}
	
    public static GregorianCalendar parseDate(String input) throws Exception {
        if (input == null) {
            throw new Exception("The date value is not recognized it is empty");
        }
        if (input.length() != 10) {
            throw new Exception(
                    "The date value is not recognized because its length is invalid.");
        }
        Integer month = new Integer(input.substring(5, 7));

        if (month < 1 || month > 12) {
            throw new Exception(
                    "The month " + month
                    + " is not regonized as a valid range for a month.");
        }
        month = month - 1;
        Integer day = new Integer(input.substring(8, 10));
        Integer year = new Integer(input.substring(0, 4));
        
        if (day < 1 || day > 31) {
            throw new Exception(
                    "The day " + day
                    + " is not regonized as a valid range for a day.");
        }
        
        GregorianCalendar gc = new GregorianCalendar(year, month, day);
        String checkDate = parseDate(gc);

        if (!checkDate.equals(input)) {
            throw new Exception(
                    "The date " + input
                    + " is invalid because it translates to " + checkDate);
        }
        return gc;
    }
    
    public static String parseDate(java.util.Date date) throws Exception {
        GregorianCalendar gc = new GregorianCalendar();

        gc.setTime(date);
        return parseDate(gc);
    }
    
    public static String parseDate(GregorianCalendar gc) throws Exception {
        if (gc == null) {
            return "";
        }
        int year = gc.get(Calendar.YEAR);
        int month = gc.get(Calendar.MONTH) + 1;
        int day = gc.get(Calendar.DAY_OF_MONTH);
        
        String monthText;
        String dayText;

        if (month < 10) { 
            monthText = "0" + new Integer(month).toString();
        } else { 
            monthText = new Integer(month).toString();
        }
        
        if (day < 10) { 
            dayText = "0" + new Integer(day).toString();
        } else { 
            dayText = new Integer(day).toString();
        }
        
        String date = new Integer(year).toString() + "-" + monthText + "-"
                + dayText;

        return date;
    }

}
